<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>商城管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品口味</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="5">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加口味</el-button
          >
        </el-col>
      </el-row>

      <!-- 口味列表区域 -->
      <el-table :data="FlavorList" border stripe>
        <!-- <el-table-column type="index"></el-table-column> -->
        <el-table-column label="口味名称" prop="name"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeFlavorById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <!-- <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNo"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination> -->
    </el-card>
    <!-- 添加口味的对话框 -->
    <el-dialog
      title="添加口味"
      :visible.sync="addDialogVisible"
      width="30%"
      @close="addDialogClosed"
    >
      <!-- 内容主体区域 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="80px"
      >
        <el-form-item label="口味名称" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addFlavor">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改口味的对话框 -->
    <el-dialog
      title="修改口味"
      :visible.sync="editDialogVisible"
      width="30%"
      @close="editDialogClosed"
    >
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="80px"
      >
        <el-form-item label="口味名字" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 获取口味列表的参数对象
      // queryInfo: {
      //   query: "",
      //   // 当前的页数
      //   pageNo: 1,
      //   // 当前每页显示多少条数据
      //   pageSize: 2,
      // },
      FlavorList: [],
      total: 0,
      addForm: {
        name: "",
      },
      editForm: {
        name: "",
      },
      addDialogVisible: false,
      editDialogVisible: false,
      addFormRules: {
        // 验证表单是否合法
        name: [{ required: true, message: "请输入商品分类名称", trigger: "blur" }],
      },
      editFormRules: {
        // 验证表单是否合法
        name: [{ required: true, message: "请输入商品分类名称", trigger: "blur" }],
      }
    };
  },
  created() {
    //获取口味列表
    this.getFlavor();
  },
  methods: {
    //获取口味列表
    async getFlavor() {
      const { data: res } = await this.$http.get(
        "Flavor/listFlavor"
        // {
        //   params: this.queryInfo,
        // }
      );
      console.log(res);
      if (res.code !== 200) {
        return this.$message.error("获取商品口味列表失败！");
      }
      this.FlavorList = res.data.flavors;
      return;
    },
    // 监听 pageSize 改变的事件
    // handleSizeChange(newSize) {
    //   // console.log(newSize)
    //   this.queryInfo.pageSize = newSize;
    //   // this.getCommodityType();
    // },
    // // 监听 页码值 改变的事件
    // handleCurrentChange(newPage) {
    //   console.log(newPage);
    //   this.queryInfo.pageNo = newPage;
    //   // this.getCommodityType();
    // },
    // 监听添加口味对话框的关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    // 点击按钮，添加新口味
    addFlavor() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        // // 可以发起添加口味的网络请求
        const { data: res } = await this.$http.post(
          "Flavor/addFlavor",
          this.addForm
        );

        if (res.code !== 200) {
          return this.$message.error("添加口味失败！");
        }

        this.$message.success("添加口味成功！");
        // 隐藏添加口味的对话框
        this.addDialogVisible = false;
        // 重新获取口味口味列表数据
        this.getFlavor();
      });
    },
    // 展示编辑口味的对话框
    async showEditDialog(id) {
      console.log(id);
      const { data: res } = await this.$http.get(
        "Flavor/getFlavorById?id=" + id
      );

      if (res.code !== 200) {
        return this.$message.error("查询口味信息失败！");
      }

      this.editForm.id = res.data.flavor.id;
      this.editForm.name = res.data.flavor.name;
      this.editDialogVisible = true;
    },
    // 监听修改口味对话框的关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },
    // 修改口味信息并提交
    editUserInfo() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return;
        // 发起修改口味信息的数据请求
        const { data: res } = await this.$http.post(
          "Flavor/updateFlavor",
          this.editForm
        );

        if (res.code !== 200) {
          return this.$message.error("修改口味失败！");
        }

        // 关闭对话框
        this.editDialogVisible = false;
        // 刷新数据列表
        this.getFlavor();
        // 提示修改成功
        this.$message.success("修改口味成功！");
      });
    },
    // 根据Id删除对应的口味信息
    async removeFlavorById(id) {
      console.log(id);
      // 弹框询问口味是否删除数据
      const confirmResult = await this.$confirm(
        "此操作将永久删除该口味, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);

      // 如果口味确认删除，则返回值为字符串 confirm
      // 如果口味取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }

      let params = new FormData();
      params.append("id", id);
      //调用删除接口
      const { data: res } = await this.$http.post("Flavor/deleteFlavor", params);

      if (res.code !== 200) {
        this.$message.error(res.massage);
        this.getFlavor();
        return;
      }

      this.$message.success("删除口味成功！");
      this.getFlavor();
    },
  },
};
</script>

<style lang="less" scoped>
.button:hover {
  background-color: rgb(64, 158, 255);
  color: rgb(255, 255, 255);
}
</style>
